package at.codebase.db.entities;

import static at.codebase.db.OfyService.ofy;

import java.util.List;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Ref;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;

/**
 * @author Lukas Pfeifhofer <lukas.pfeifhofer@devlabs.pro>
 */
@Entity
public class EApiPartner {
	@Id
	private Long partnerId;

	private String appName;
	// Must NOT end with a '/' !
	private String appUrl;
	private String comment;
	
	private String accessToken;
	
	private EApiPartner(){}
	
	private EApiPartner(String appName, String appUrl, String comment, String accessToken) {
		this.appName = appName;
		this.appUrl = appUrl;
		this.comment = comment;
		this.accessToken = accessToken;
	}
	
	/*
	 * Objectify
	 */
	@SuppressWarnings("boxing")
	public static Key<EApiPartner> key(Long partnerId) {
		return Key.create(EApiPartner.class, partnerId);
	}
	
	public static Ref<EApiPartner> ref(Long partnerId) {
		Key<EApiPartner> key = key(partnerId);
		return Ref.create(key);
	}
	
	public static Ref<EApiPartner> ref(EApiPartner partner) {
		return Ref.create(partner);
	}
	
	public static EApiPartner createPartner(String appName, String accessToken, String appUrl, String comment){
		return new EApiPartner(appName, appUrl, comment, accessToken);
	}

	public Long getPartnerId() {
		return partnerId;
	}

	public String getAppName() {
		return appName;
	}

	public String getAppUrl() {
		return appUrl;
	}

	public String getComment() {
		return comment;
	}

	public String getAccessToken() {
		return accessToken;
	}

	public static List<EApiPartner> getAllPartners() {
		return ofy().load().type(EApiPartner.class).list();
	}

	public void setAccessToken(String partnerAccessKey) {
		this.accessToken = partnerAccessKey;
	}

	public void setAppUrl(String partnerUrl) {
		this.appUrl = partnerUrl;
	}
	
}
